package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/upload")
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils;
//    @PostMapping("/upload")
//    public Result upload(String name, String age, MultipartFile image) throws Exception{
//        log.info("文件上传参数:{},{},{}",name,age,image);
//        String originalFilename = image.getOriginalFilename();
//        int index = originalFilename.lastIndexOf(".");
//        String newFieName = UUID.randomUUID().toString()+originalFilename.substring(index);
//        image.transferTo(new File("E:\\images\\"+newFieName ));
//        return Result.success();
//    }
    @PostMapping
    public Result upload(MultipartFile image) throws IOException {
        String uploadUrl = aliOSSUtils.upload(image);
        log.info("上传的图片URL:{}",uploadUrl);
        return Result.success(uploadUrl);
    }
}
